<template>
  <el-dialog
    :title="!dataForm.gradeId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="等级称号名称" prop="gradeName">
      <el-input v-model="dataForm.gradeName" placeholder="等级称号名称"></el-input>
    </el-form-item>
    <el-form-item label="所属榜单分类id" prop="catId">
      <el-input v-model="dataForm.catId" placeholder="所属榜单分类id"></el-input>
    </el-form-item>
    <el-form-item label="所属榜单分类名称" prop="catName">
      <el-input v-model="dataForm.catName" placeholder="所属榜单分类名称"></el-input>
    </el-form-item>
    <el-form-item label="榜单id" prop="rankId">
      <el-input v-model="dataForm.rankId" placeholder="榜单id"></el-input>
    </el-form-item>
    <el-form-item label="榜单名称" prop="rankName">
      <el-input v-model="dataForm.rankName" placeholder="榜单名称"></el-input>
    </el-form-item>
    <el-form-item label="等级排序" prop="sort">
      <el-input v-model="dataForm.sort" placeholder="等级排序"></el-input>
    </el-form-item>
    <el-form-item label="等级需要的投票数" prop="growthVoteCount">
      <el-input v-model="dataForm.growthVoteCount" placeholder="等级需要的投票数"></el-input>
    </el-form-item>
    <el-form-item label="是否为默认等级:0否，1是" prop="isDefault">
      <el-input v-model="dataForm.isDefault" placeholder="是否为默认等级:0否，1是"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="note">
      <el-input v-model="dataForm.note" placeholder="备注"></el-input>
    </el-form-item>
    <el-form-item label="是否删除" prop="isDelete">
      <el-input v-model="dataForm.isDelete" placeholder="是否删除"></el-input>
    </el-form-item>
    <el-form-item label="创建人id" prop="createUserId">
      <el-input v-model="dataForm.createUserId" placeholder="创建人id"></el-input>
    </el-form-item>
    <el-form-item label="创建人名称" prop="createUserName">
      <el-input v-model="dataForm.createUserName" placeholder="创建人名称"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          gradeId: 0,
          gradeName: '',
          catId: '',
          catName: '',
          rankId: '',
          rankName: '',
          sort: '',
          growthVoteCount: '',
          isDefault: '',
          note: '',
          isDelete: '',
          createUserId: '',
          createUserName: '',
          createTime: '',
          updateTime: ''
        },
        dataRule: {
          gradeName: [
            { required: true, message: '等级称号名称不能为空', trigger: 'blur' }
          ],
          catId: [
            { required: true, message: '所属榜单分类id不能为空', trigger: 'blur' }
          ],
          catName: [
            { required: true, message: '所属榜单分类名称不能为空', trigger: 'blur' }
          ],
          rankId: [
            { required: true, message: '榜单id不能为空', trigger: 'blur' }
          ],
          rankName: [
            { required: true, message: '榜单名称不能为空', trigger: 'blur' }
          ],
          sort: [
            { required: true, message: '等级排序不能为空', trigger: 'blur' }
          ],
          growthVoteCount: [
            { required: true, message: '等级需要的投票数不能为空', trigger: 'blur' }
          ],
          isDefault: [
            { required: true, message: '是否为默认等级:0否，1是不能为空', trigger: 'blur' }
          ],
          note: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ],
          isDelete: [
            { required: true, message: '是否删除不能为空', trigger: 'blur' }
          ],
          createUserId: [
            { required: true, message: '创建人id不能为空', trigger: 'blur' }
          ],
          createUserName: [
            { required: true, message: '创建人名称不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.gradeId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.gradeId) {
            this.$http({
              url: this.$http.adornUrl(`/rank/membergrade/info/${this.dataForm.gradeId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.gradeName = data.memberGrade.gradeName
                this.dataForm.catId = data.memberGrade.catId
                this.dataForm.catName = data.memberGrade.catName
                this.dataForm.rankId = data.memberGrade.rankId
                this.dataForm.rankName = data.memberGrade.rankName
                this.dataForm.sort = data.memberGrade.sort
                this.dataForm.growthVoteCount = data.memberGrade.growthVoteCount
                this.dataForm.isDefault = data.memberGrade.isDefault
                this.dataForm.note = data.memberGrade.note
                this.dataForm.isDelete = data.memberGrade.isDelete
                this.dataForm.createUserId = data.memberGrade.createUserId
                this.dataForm.createUserName = data.memberGrade.createUserName
                this.dataForm.createTime = data.memberGrade.createTime
                this.dataForm.updateTime = data.memberGrade.updateTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/rank/membergrade/${!this.dataForm.gradeId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'gradeId': this.dataForm.gradeId || undefined,
                'gradeName': this.dataForm.gradeName,
                'catId': this.dataForm.catId,
                'catName': this.dataForm.catName,
                'rankId': this.dataForm.rankId,
                'rankName': this.dataForm.rankName,
                'sort': this.dataForm.sort,
                'growthVoteCount': this.dataForm.growthVoteCount,
                'isDefault': this.dataForm.isDefault,
                'note': this.dataForm.note,
                'isDelete': this.dataForm.isDelete,
                'createUserId': this.dataForm.createUserId,
                'createUserName': this.dataForm.createUserName,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
